package com.wc.AlgoOJ_train.train6_图论.最短路.套利;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.*;

/**
 * @Author congge
 * @Date 2024/4/14 23:41
 * @description http://43.138.190.70:8888/p/GR0211
 */
public class Main {
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int N = 110, M = N * (N - 1);
    static int[] h = new int[N];
    static int[] e = new int[M];
    static int[] ne = new int[M];
    static double[] w = new double[M];
    static double[] dist = new double[N];
    static boolean[] st = new boolean[N];
    static int[] cnt = new int[N];
    static int n, m;
    static int idx = 0;

    public static void main(String[] args) {
        int step = 0;
        while (sc.hasNext()) {
            step++;
            n = sc.nextInt();
            if (n == 0) break;
            Map<String, Integer> map = new HashMap<>();
            h = new int[n + 1];
            idx = 1;
            for (int i = 1; i <= n; i++) {
                String name = sc.next();
                map.put(name, i);
            }
            m = sc.nextInt();
            e = new int[m + 1];
            ne = new int[m + 1];
            w = new double[m + 1];
            while (m-- > 0) {
                int a = map.get(sc.next());
                double c = sc.nextDouble();
                int b = map.get(sc.next());
                add(a, b, c);
            }
            boolean res = spfa();
            out.printf("Case %d: %s\n", step, res ? "Yes" : "No");
        }
        out.flush();
    }

    static void add(int a, int b, double c) {
        e[idx] = b;
        w[idx] = c;
        ne[idx] = h[a];
        h[a] = idx++;
    }

    static boolean spfa() {
        Arrays.fill(dist, 1, n + 1, 0);
        Arrays.fill(st, 1, n + 1, false);
        Arrays.fill(cnt, 1, n + 1, 0);
        Queue<Integer> q = new LinkedList<>();
        q.add(1);
        st[1] = true;
        dist[1] = 1;
        while (!q.isEmpty()) {
            int t = q.poll();
            st[t] = false;
            for (int i = h[t]; i > 0; i = ne[i]) {
                int j = e[i];
                if (dist[j] < dist[t] * w[i]) {
                    dist[j] = dist[t] * w[i];
                    if (!st[j]) {
                        cnt[j] = cnt[t] + 1;
                        q.add(j);
                        st[j] = true;
                        if (cnt[j] >= n || dist[1] > 1) return true;
                    }
                }
            }
        }
        return false;
    }
}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}
